package com.sc.study.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sc.study.dao.StudyNoteMapper;
import com.sc.study.dto.ArticleQueryDTO;
import com.sc.study.entity.StudyArticle;
import com.sc.study.dao.StudyArticleMapper;
import com.sc.study.entity.StudyNote;
import com.sc.study.service.StudyArticleService;
import com.sc.study.vo.PageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 学习文章服务impl
 *
 * @author 徐赫阳
 * @date 2021/09/27
 */
@Service
@Component
public class StudyArticleServiceImpl extends ServiceImpl<StudyArticleMapper, StudyArticle> implements StudyArticleService {

    /**
     * 研究文章映射器
     */
    @Autowired
    private StudyArticleMapper studyArticleMapper;


    /**
     * 得到的文章列表
     *
     * @param queryDTO 查询dto
     * @return {@link PageVO}<{@link StudyArticle}>
     */
    @Override
    public PageVO<StudyArticle> getArticleList(ArticleQueryDTO queryDTO){
        QueryWrapper<StudyArticle> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .like(queryDTO.getContent() != null,StudyArticle::getTitle,queryDTO.getContent()).or()
                .like(queryDTO.getContent() != null,StudyArticle::getContent,queryDTO.getContent())
                .orderByDesc(StudyArticle::getId);
        Integer count = this.studyArticleMapper.selectList(queryWrapper).size();
        List<StudyArticle> list = this.studyArticleMapper.selectList(queryWrapper.lambda().last("LIMIT "+ (queryDTO.getPage() - 1) * queryDTO.getPageSize() + ","+ queryDTO.getPageSize()));
        return PageVO.setPage(queryDTO.getPage(),queryDTO.getPageSize(),list,count);
    }
}
